///************************************************************************
/// <copyrigth>2018-2019 Corporation.All Rights Reserved</copyrigth>
/// <author>tangyingzhong</author>
/// <contact>tangyz114987@outlook.com</contact>
/// <version>v1.0.0</version>
/// <describe>
/// It support file's common operations on different os platform
///</describe>
/// <date>2021/1/31</date>
///***********************************************************************
#ifndef FILE_H
#define FILE_H

#include <time.h>
#include <atomic>
#include <string>
#include <fstream>
#include <iostream>
#include "boost/filesystem.hpp"

namespace SystemTool
{
	namespace IO
	{
		enum OpenMode
		{
			FILE_READ = std::ios::in,

			FILE_WRITE = std::ios::out,

			FILE_APPEND = std::ios::app,

			FILE_TRUNC = std::ios::trunc
		};

		enum SeekOrigin
		{
			FILE_SEEK_BEGIN = std::ios::beg,

			FILE_SEEK_CUR = std::ios::cur,

			FILE_SEEK_END = std::ios::end
		};

		class File
		{
		public:
			// Construct the file
			File();

			// Destory the file
			~File();

		public:
			// Open a file 
			bool Open(std::string strFilePath, OpenMode eOpenMode);

			// Read the file 
			bool Read(char* pBuffer, int iReadSize);

			// Write data to the file
			bool Write(const char* pBuffer, int iWriteSize);

			// Close the file
			void Close();

			// Seek the file
			void Seek(SeekOrigin eSeekPos, int iOffset);

			// Flush the file
			void Flush();

		public:
			// Get file size
			static unsigned long long GetFileSize(std::string strFilePath);

			// Remane the file
			static bool Rename(std::string strOldFilePath, std::string strNewFilePath);

			// Create new file recursively
			static bool Create(std::string strFilePath);

			// Copy the file recursively
			static bool Copy(std::string strSrcFilePath, std::string strDestFilePath, bool bIsCover = false);

			// Delete the file recursively
			static bool Delete(std::string strFilePath);

			// Is the file existed or not
			static bool IsExisted(std::string strFilePath);

			// Get last wite time of file
			static std::time_t GetLastWriteTime(std::string strFilePath);

			// Set last wite time of file
			static bool SetLastWriteTime(std::string strFilePath, std::time_t iNewTime);

			// Get rooth path of the file
			static std::string GetRootPath(std::string strFilePath);

			// Get parent path of the file
			static std::string GetParentPath(std::string strFilePath);

		private:
			// Get the IsReadMode
			inline bool GetIsReadMode() const
			{
				return m_bIsReadMode;
			}

			// Set the IsReadMode
			inline void SetIsReadMode(bool bIsReadMode)
			{
				m_bIsReadMode = bIsReadMode;
			}

			// Get the Disposed
			inline bool GetDisposed() const
			{
				return m_bDisposed;
			}

			// Set the Disposed
			inline void SetDisposed(bool bDisposed)
			{
				m_bDisposed = bDisposed;
			}

		private:
			// File streamer
			std::fstream m_FileStreamer;

			// File mode
			std::atomic<bool> m_bIsReadMode;

			// Is object disposed
			std::atomic<bool> m_bDisposed;
		};
	}
}

#endif // FILE_H
